#ifndef CROWBAR_SCHEDULER_H_INCLUDED
#define CROWBAR_SCHEDULER_H_INCLUDED

#include "runtime.h"
#include "queue.h"


typedef bool (*schedproc_t)(void * arg);


class Scheduler_t
{
private:
	static const unsigned int WaitBgThreadTimeout = 1000;
	static const unsigned int DefaultInterval = 10;
	
	struct SchedulerEntry
	{
		schedproc_t proc;
		void * arg;
		const char * tagline;
		unsigned int interval;
		unsigned int ticks;
	};
	
	Queue<SchedulerEntry, 512> queue;
	Queue<SchedulerEntry, 128> bgqueue;
	bool m_active;
	HANDLE hthread;
	HANDLE hbgevent;
	
	void Work();
	static DWORD WINAPI WorkBg(Scheduler_t& self);

public:
	unsigned long cycles;
	Scheduler_t();
	
	bool Start();
	bool Stop();
	bool IsActive() const;
	
	bool Register(schedproc_t func, void * arg);
	bool Register(schedproc_t func, void * arg, const char * tagline);
	bool Register(schedproc_t func, void * arg, unsigned int interval);
	bool Register(schedproc_t func, void * arg, unsigned int interval, const char * tagline);
	bool RegisterBg(schedproc_t func, void * arg);
	bool RegisterBg(schedproc_t func, void * arg, const char * tagline);
};

extern Scheduler_t Scheduler;



#endif // CROWBAR_SCHEDULER_H_INCLUDED